package com.hhkj.study.admin.article.service.biz.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.hhkj.study.admin.article.entity.Article;
import com.hhkj.study.admin.article.entity.ArticleExamFlow;
import com.hhkj.study.admin.article.model.podo.ArticleExamFlowDo;
import com.hhkj.study.admin.article.model.podo.ArticleExamFlowPo;
import com.hhkj.study.admin.article.model.req.ArticleExamFlowReq;
import com.hhkj.study.admin.article.model.res.ArticleExamFlowRes;
import com.hhkj.study.admin.article.service.base.ArticleExamFlowService;
import com.hhkj.study.admin.article.service.base.ArticleService;
import com.hhkj.study.admin.article.service.biz.ArticleExamFlowBizService;
import com.hhkj.study.admin.article.service.extend.ArticleExamFlowExtendService;
import com.hhkj.study.admin.dict.service.extend.DictMultiLevelExtendService;
import com.hhkj.study.common.cache.GlobalCache;
import com.hhkj.study.common.handler.CommonHandler;
import com.ruoyi.common.base.biz.service.biz.BaseBizServiceImpl;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.cont.SysConst;
import com.ruoyi.common.enums.DictCodeEnum;
import com.ruoyi.common.exception.GlobalException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* ArticleExamFlowBizServiceImpl
*
* @author SERVER
* @since 2024-09-06
*/
@Service
public class ArticleExamFlowBizServiceImpl extends BaseBizServiceImpl<ArticleExamFlow, ArticleExamFlowReq, ArticleExamFlowRes, ArticleExamFlowDo, ArticleExamFlowPo, ArticleExamFlowService, ArticleExamFlowExtendService> implements ArticleExamFlowBizService {

    @Resource
    private ArticleService articleService;

    @Resource
    private GlobalCache globalCache;

    @Override
    protected void afterGetById(ArticleExamFlowRes articleExamFlowRes) {
        supplyArticleFlow(Lists.newArrayList(articleExamFlowRes));
    }

    @Override
    protected void afterPage(List<ArticleExamFlowRes> articleExamFlowResList) {
        supplyArticleFlow(articleExamFlowResList);
    }

    @Override
    protected void afterList(List<ArticleExamFlowRes> articleExamFlowResList) {
        supplyArticleFlow(articleExamFlowResList);
    }

    private void supplyArticleFlow(List<ArticleExamFlowRes> articleExamFlowResList) {
        if (CollectionUtil.isEmpty(articleExamFlowResList)) {
            return;
        }

        // 通过资讯id获取结果集中所有的资讯组装成Map
        List<Long> articleIdList = articleExamFlowResList.stream().flatMap(a -> a.getArticleIdList().stream()).collect(Collectors.toList());
        LambdaQueryWrapper<Article> articleQuery = new LambdaQueryWrapper<>();
        articleQuery.in(Article::getId, articleIdList).select(Article::getId, Article::getTitle);
        List<Article> articleList = articleService.list(articleQuery);
        Map<Long, String> articleMap = Maps.newHashMap();
        if (CollectionUtil.isNotEmpty(articleList)) {
            articleMap = articleList.stream().collect(Collectors.toMap(Article::getId, Article::getTitle));
        }

        Map<Long, String> finalArticleMap = articleMap;
        Map<Long, String> dictMap = globalCache.cacheDictMap(Constants.REGION_COURSE_CODE_LIST);
        articleExamFlowResList.forEach(articleExamFlowRes -> {
            articleExamFlowRes.setArticleName(CommonHandler.joinStr(finalArticleMap, articleExamFlowRes.getArticleIdList(), Constants.VERTICAL_LINE));
            articleExamFlowRes.setArticleMap(CommonHandler.getMapByDict(finalArticleMap, articleExamFlowRes.getArticleIdList()));
            articleExamFlowRes.setProvinceName(CommonHandler.joinStr(dictMap, articleExamFlowRes.getProvinceIdList(), Constants.VERTICAL_LINE));
            articleExamFlowRes.setProvinceMap(CommonHandler.getMapByDict(dictMap, articleExamFlowRes.getProvinceIdList()));
            articleExamFlowRes.setCourseTypeName(CommonHandler.joinStr(dictMap, articleExamFlowRes.getCourseTypeList(), Constants.VERTICAL_LINE));
            articleExamFlowRes.setCourseTypeMap(CommonHandler.getMapByDict(dictMap, articleExamFlowRes.getCourseTypeList()));
        });
    }

    @Override
    public boolean updatePublishFlag(ArticleExamFlowReq req) {
        ArticleExamFlow articleExamFlow = this.getExtendService().getByIdNotNull(req.getId());
        if (articleExamFlow.getPublishedFlag().equals(req.getPublishedFlag())) {
            throw new GlobalException(SysConst.Flag.IS.getFlag().equals(req.getPublishedFlag()) ?
                    "已上架" : "已下架");
        }
        LambdaUpdateWrapper<ArticleExamFlow> articleFlowUpdate = new LambdaUpdateWrapper<>();
        articleFlowUpdate.eq(ArticleExamFlow::getId, req.getId())
                .set(ArticleExamFlow::getPublishedFlag, req.getPublishedFlag());
        return this.getService().update(articleFlowUpdate);
    }
}